package com.sq.partner.manager.util;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.mybatis.model.DeptDto;

public class Page<T> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 默认页码 */
	private static final int DEFAULT_PAGE_NUMBER = 1;

	/** 默认每页记录数 */
	private static final int DEFAULT_PAGE_SIZE = 10;

	/** 最大每页记录数 */
	private static final int MAX_PAGE_SIZE = 500;

	private Integer current;// 当前页
	private Integer size;// 每页条数
	private List<T> data;// 当前页数据
	private long total;// 总记录数
	private int maxPage = 0;
	private String result = "success";
	private String message = "";

	/** 机构 */
	private List<DeptDto> depts;
	/**
	 * 表示page的类型
	 */
	private T type;
	private Object obj;
	private Map<String, Object> map;

	public Page() {
		this.size = DEFAULT_PAGE_SIZE;
		this.current = DEFAULT_PAGE_NUMBER;
	};

	public List<DeptDto> getDepts() {
		return depts;
	}

	public void setDepts(List<DeptDto> depts) {
		this.depts = depts;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getType() {
		return type;
	}

	public void setType(T type) {
		this.type = type;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	/**
	 * 初使化 使用默认的大小 Constants.PAGE_SIZE
	 * 
	 * @param current
	 * @param total
	 */
	public Page(int current, long total) {
		this.size = Constants.PAGE_SIZE;
		this.maxPage = getMaxPage(total, size);
		this.total = total;
		if (current < 1) {
			this.current = 1;
		} else if (current > maxPage) {
			this.current = maxPage;
		} else {
			this.current = current;
		}
	}

	public void setSize(Integer size) {
		if (null == size || size < 1 || size > MAX_PAGE_SIZE) {
			this.size = DEFAULT_PAGE_SIZE;
		} else {
			this.size = size;
		}

	}

	public void setCurrent(Integer current) {
		if (null == current || current < 1) {
			this.current = DEFAULT_PAGE_NUMBER;
		} else {
			this.current = current;
		}

	}

	public void setTotal(long total) {
		this.maxPage = getMaxPage(total, size);
		this.total = total;
	}

	public Page(int current, long total, int size) {
		this.size = size;
		this.maxPage = getMaxPage(total, size);
		this.total = total;
		if (current < 1) {
			this.current = 1;
		} else if (current > maxPage) {
			this.current = maxPage;
		} else {
			this.current = current;
		}
	}

	/**
	 * 当前页
	 * 
	 * @return
	 */
	public int getCurrent() {
		return current;
	}

	/**
	 * 数据
	 * 
	 * @return
	 */
	public List<T> getData() {
		return data;
	}

	/**
	 * 设置数据
	 * 
	 * @param data
	 */
	public void setData(List<T> data) {
		this.data = data;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	/**
	 * 每页记录条数
	 * 
	 * @return
	 */
	public int getSize() {
		return size;
	}

	/**
	 * 总记录数
	 * 
	 * @return
	 */
	public long getTotal() {
		return total;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public int getNext() {
		return current + 1 > maxPage ? maxPage : current + 1;
	}

	/**
	 * 上一页
	 * 
	 * @return
	 */
	public int getPre() {
		return current - 1 < 1 ? 1 : current - 1;
	}

	/**
	 * 最后一页
	 * 
	 * @return
	 */
	public int getLast() {
		return maxPage;
	}

	/**
	 * 是否第一页
	 * 
	 * @return
	 */
	public boolean getIsFirst() {
		return current <= 1 ? true : false;
	}

	/**
	 * 是否第一页
	 * 
	 * @return
	 */
	public boolean getIsLast() {
		return current >= maxPage ? true : false;
	}

	/**
	 * 获取当前页第一条记录对应的条数
	 * 
	 * @return
	 */
	public long getFirstRecodeIndex() {
		return (current - 1) * size + 1;
	}

	/**
	 * 获取当前页最后一条记录对应的条数
	 * 
	 * @return
	 */
	public long getLastRecodeIndex() {
		return current * size;
	}

	private static int getMaxPage(long total, int size) {
		if (total % size == 0) {
			return (int) total / size;
		} else {
			return (int) total / size + 1;
		}

	}

	public static void main(String[] args) {
		Object[] os = { "d", "f" };
		System.out.println(os instanceof Object[]);
		System.out.println(getMaxPage(6, 3));
	}
}
